﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormSetup.cs" company="">
//   
// </copyright>
// <summary>
//   The form setup.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace NetworkSupervisor
{
    using System;
    using System.Linq;
    using System.Windows.Forms;

    using DevExpress.XtraEditors;
    using DevExpress.XtraGrid.Views.Base;

    using NetworkSupervisor.Model.NetworkSupervisor;

    /// <summary>
    ///   The form setup.
    /// </summary>
    public partial class FormSetup : DevExpress.XtraEditors.XtraForm
    {
        #region Constructors and Destructors

        /// <summary>
        ///   Initializes a new instance of the <see cref="FormSetup" /> class.
        /// </summary>
        public FormSetup()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>The form setup_ load.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void FormSetup_Load(object sender, EventArgs e)
        {
        }

        /// <summary>The data navigator 2_ button click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        private void dataNavigator2_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.ButtonType)
            {
                case NavigatorButtonType.Custom:
                    break;
                case NavigatorButtonType.First:
                    break;
                case NavigatorButtonType.PrevPage:
                    break;
                case NavigatorButtonType.Prev:
                    break;
                case NavigatorButtonType.Next:
                    break;
                case NavigatorButtonType.NextPage:
                    break;
                case NavigatorButtonType.Last:
                    break;
                case NavigatorButtonType.Append:
                    break;
                case NavigatorButtonType.Remove:
                    e.Handled = true;
                    if (this.gridView2.RowCount < 1)
                    {
                        break;
                    }

                    DialogResult result = XtraMessageBox.Show("是否确认删除类别", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (result == DialogResult.OK)
                    {
                        (this.gridView2.GetFocusedRow() as ta类别).Delete();
                    }

                    break;
                case NavigatorButtonType.Edit:
                    break;
                case NavigatorButtonType.EndEdit:
                    XtraMessageBox.Show("保存成功");
                    break;
                case NavigatorButtonType.CancelEdit:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        /// <summary>The data navigator 3_ button click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        private void dataNavigator3_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.ButtonType)
            {
                case NavigatorButtonType.Custom:
                    break;
                case NavigatorButtonType.First:
                    break;
                case NavigatorButtonType.PrevPage:
                    break;
                case NavigatorButtonType.Prev:
                    break;
                case NavigatorButtonType.Next:
                    break;
                case NavigatorButtonType.NextPage:
                    break;
                case NavigatorButtonType.Last:
                    break;
                case NavigatorButtonType.Append:
                    {
                        e.Handled = true;
                        if (this.gridView2.RowCount < 1)
                        {
                            return;
                        }

                        ta类别 lb = this.gridView2.GetFocusedRow() as ta类别;
                        ta网址 item = new ta网址(this.session1);
                        item.类别 = lb.类别;
                        item.Save();
                        this.XPC网址.Add(item);
                    }

                    break;
                case NavigatorButtonType.Remove:
                    {
                        e.Handled = true;
                        if (this.gridView3.RowCount < 1)
                        {
                            break;
                        }

                        DialogResult result = XtraMessageBox.Show("是否确认删除网址", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (result == DialogResult.OK)
                        {
                            ta网址 item = this.gridView3.GetFocusedRow() as ta网址;
                            string sql = string.Format("DELETE FROM ta搜索结果 WHERE 类别='{0}' AND 关键字='{1}'", item.类别, item.网址);
                            this.session1.ExecuteNonQuery(sql);
                            item.Delete();
                        }
                    }

                    break;
                case NavigatorButtonType.Edit:
                    break;
                case NavigatorButtonType.EndEdit:
                    XtraMessageBox.Show("保存成功");
                    break;
                case NavigatorButtonType.CancelEdit:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        /// <summary>The grid view 2_ focused row changed.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void gridView2_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (this.gridView2.RowCount < 1)
            {
                return;
            }

            ta类别 item = this.gridView2.GetFocusedRow() as ta类别;
            this.XPC网址.CriteriaString = string.Format("类别='{0}'", item.类别);
            if ((new[] { "关注", "敏感" }).Contains(item.类别))
            {
                this.gridView3.Columns["网址"].Caption = "关键字";
                this.gridView3.Columns["名称"].Visible = false;
            }
            else
            {
                this.gridView3.Columns["网址"].Caption = "网址";
                this.gridView3.Columns["名称"].Visible = true;
            }
        }

        #endregion
    }
}